<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['generator:page:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['generator:page:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['generator:page:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['generator:page:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="页面id" align="center" prop="pageId" />
      <el-table-column label="业务对象id" align="center" prop="tableId" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="描述" align="center" prop="descText" />
      <el-table-column label="配置数据" align="center" prop="config" />
      <el-table-column label="版本号" align="center" prop="version" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['generator:page:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['generator:page:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改pageDesign对话框 -->
    
    <el-dialog :title="title" :visible.sync="open"  fullscreen append-to-body @closed="diaClose" >
      <pageDesign label-width="80px" :bizObjList="this.bizObjList" ref="child" />  
    </el-dialog>
    
  </div>
</template>

<script>
import { getGenTable, updateGenTable, getBizObjList } from "@/api/tool/gen";
import { optionselect as getDictOptionselect } from "@/api/system/dict/type";
import { listMenu as getMenuTreeselect } from "@/api/system/menu";
import { listPage, getPage, delPage, addPage, updatePage } from "@/api/tool/gen";
import pageDesign from '../design/index';

export default {
  name: "Page",
  components: { pageDesign },
  
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // pageDesign表格数据
      pageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tableId: null,
        name: null,
        descText: null,
        config: null,
        version: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 表信息
      tables: [],
      // 表列信息
      columns: [],
      // 字典信息
      dictOptions: [],
      // 菜单信息
      menus: [],
      // 表详细信息
      info: {},
      tableId: "",
      bizObjList:{}
    };
  },
  created() {
    const tableId = this.$route.params && this.$route.params.tableId;
    this.tableId = tableId
    if (tableId) {
      // 获取表详细信息
      getGenTable(tableId).then(res => {
        this.columns = res.data.cols;
        this.info = res.data.info;
        this.tables = res.data.tables;
      });
      // getOtherTable(tableId).then(res => {
      //   this.columns = res.data.cols;
      //   this.info = res.data.info;
      //   this.tables = res.data.tables;
      // });
      /** 查询字典下拉列表 */
      getDictOptionselect().then(response => {
        this.dictOptions = response.data;
      });
      /** 查询菜单下拉列表 */
      getMenuTreeselect().then(response => {
        this.menus = this.handleTree(response.data, "menuId");
      });
    }
    this.queryParams.tableId=tableId
    this.getList();
  },
  methods: {
    /** 查询pageDesign列表 */
    getList() {
      this.loading = true;
      listPage(this.queryParams).then(response => {
        this.pageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      // this.form = {
      //   pageId: null,
      //   tableId: null,
      //   name: null,
      //   descText: null,
      //   config: null,
      //   createBy: null,
      //   createTime: null,
      //   updateBy: null,
      //   updateTime: null,
      //   version: null
      // };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.pageId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      this.bizObjList = {}
      this.open = true;
      this.title = "页面设计";
      this.getBizObjList().then(res=>{
        this.reset();
        this.open = true;
        this.title = "页面设计";
      })
      
      // handlePageDesign()
    },

    getBizObjList(){
      return new Promise((resolve,reject)=>{
          getBizObjList(this.tableId).then(res => {
          // debugger
          this.bizObjList = res.data
          console.log(res)
          resolve(res.data)
        })
      }) 
    },

    diaClose(){
      debugger
      this.$refs.child.drawingList = []
      this.open=false
    },

    handlePageDesign(row) {
      const tableId = row.tableId || this.ids[0];
      const tableName = row.tableName || this.tableNames[0];
      const params = { obj: this.queryParams.pageNum };
      this.$tab.openPage(tableName + " 页面设计", '/tool/design/index/' + tableId, params);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const pageId = row.pageId || this.ids
      getPage(pageId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改pageDesign";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.pageId != null) {
            updatePage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const pageIds = row.pageId || this.ids;
      this.$modal.confirm('是否确认删除pageDesign编号为"' + pageIds + '"的数据项？').then(function() {
        return delPage(pageIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('generator/page/export', {
        ...this.queryParams
      }, `page_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

